package thread.exam;

import java.util.HashMap;

public class Exam_1 extends Thread {
	public static void main(String[] args) throws InterruptedException {
		new Exam_1("1").start();
		new Exam_1("1").start();
		new Exam_1("1").start();
		new Exam_1("2").start();
		new Exam_1("2").start();
		new Exam_1("2").start();
		new Exam_1("1").start();
		new Exam_1("1").start();
	}
	
	private String key;
	public Exam_1(String key){
		this.key = key;
	}
	@Override
	public void run() {
		try {
			TestDo.doSome(key);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class TestDo {
	private static HashMap<String, Object> map = new HashMap<>();
	
	public static void doSome(String key) throws InterruptedException 
	{
		Object obj = get(key);
		synchronized(obj) {
			System.out.println(key+": "+getSecond());
			Thread.sleep(1000);
		}
	}

	private static Object get(String key) {
		synchronized(map) {
			Object r = map.get(key);
			if(r==null) {
				map.put(key, new Object());
			}
			return map.get(key);
		}
	}
	
	private static long getSecond() {
		return System.currentTimeMillis() / 1000;
	}
}